package net.godbirds.boot.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Component
public class SpringUtil implements ApplicationContextAware{
	private static ApplicationContext applicationContext;
	@Autowired
	public static Environment env;
	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		if(applicationContext == null){
			applicationContext = arg0;
		}
	}
	/**
	 * 获取Spring上下文容器
	 * @return
	 */
	public static ApplicationContext getApplicationContext(){
		return applicationContext;
	}
	/**
	 * 获取环境信息
	 * @return
	 */
	public static Environment  getEnvironment(){
		if(env == null){
			env =  getApplicationContext().getEnvironment();
		}
		return env;
	}
	/**
	 * 获取前缀对应的键值对
	 * @param key
	 * @return
	 */
	public static Map<String, Object> getEnvValues(String key){
		RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(getEnvironment(), key);
		Map<String, Object> map = propertyResolver.getSubProperties(key + ".");
		return map;
	}
	/**
	 * 获取配置文件信息
	 * @param key
	 * @return
	 */
	public static String getEnvValue(String key){
		return getEnvironment().getProperty(key);
	}
	/**
	 * 获取配置文件信息
	 * @param key
	 * @param targetType
	 * @return
	 */
	public static Object getEnvValue(String key, Class<?> targetType){
		return getEnvironment().getProperty(key, targetType);
	}
	/**
	 * 根据指定名称获取Bean
	 * @param name
	 * @return
	 */
	public static Object getBean(String name){
		return getApplicationContext().getBean(name);
	}
	/**
	 * 根据指定类获取Bean
	 * @param clazz
	 * @return
	 */
	public static <T> T getBean(Class<T> clazz){
		return getApplicationContext().getBean(clazz);
	}
	/**
	 * 根据指定名称和类获取Bean
	 * @param name
	 * @param clazz
	 * @return
	 */
	public static <T> T getBean(String name,Class<T> clazz){
		return getApplicationContext().getBean(name,clazz);
	}
	/**
	 * 获取Application的名称
	 * @return
	 */
	public static String getApplicationName(){
		if(getApplicationContext() == null){
			return "There is no application!";
		}
		return getApplicationContext().getApplicationName();
	}
	/**
	 * 获取请求地址映射关系
	 * @return
	 */
	public static List<Map<String,String>> getRequestMapping(){
		List<Map<String,String>> rtv = new ArrayList<Map<String,String>>();
		RequestMappingHandlerMapping rmhm = getApplicationContext().getBean(RequestMappingHandlerMapping.class);
		Map<RequestMappingInfo,HandlerMethod> infoMap = rmhm.getHandlerMethods();
		for(RequestMappingInfo info : infoMap.keySet()){
			Map<String,String> map = new HashMap<String,String>();
			String method = infoMap.get(info).getMethod().getName();
			String[] urls = infoMap.get(info).getMethodAnnotation(RequestMapping.class).value();
			String reqName = infoMap.get(info).getMethodAnnotation(RequestMapping.class).name();
			String url = null;
			if(urls != null && urls.length > 0){
				url = ((reqName != null && !"".equals(reqName))?":":"") + urls[0].toString();
			}
			System.out.println("- SpringUtil getRequestMapping : method="+method+"  url="+url);
			map.put(url, method);
			rtv.add(map);
		}
		return rtv;
	}
}
